<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline size="small">
          <el-form-item :label="$t('status1')">
            <el-select v-model="tableFrom.status" :placeholder="$t('state')" clearable @change="getList">
              <el-option v-for="item in optionsData" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('keywords')">
            <el-input v-model="tableFrom.keyword" @keyup.enter.native="getList" :placeholder="$t('please46')"
              class="selWidth">
              <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList" />
            </el-input>
          </el-form-item>
        </el-form>
        <el-button size="small" type="primary" @click="onAdd">{{
          $t("addcustomerservice")
        }}</el-button>
      </div>
      <el-table v-loading="loading" :data="tableData.data" style="width: 100%" size="small">
        <el-table-column prop="service_id" label="ID" min-width="60" />
        <el-table-column prop="user.nickname" :label="$t('wechatusername')" min-width="130" />
        <el-table-column :label="$t('customerserviceavatar')" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image class="tabImage" :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="nickname" :label="$t('customerservicename')" min-width="130" />
        <el-table-column :label="$t('whethertodisplay')" min-width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" :active-text="$t('display')"
              :inactive-text="$t('hide')" @click.native="onchangeIsShow(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column prop="create_time" :label="$t('addtime')" min-width="150" />
        <el-table-column :label="$t('operation')" min-width="150" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="goList(scope.row.service_id, scope.$index)">{{
              $t("chatrecord")
            }}</el-button>
            <el-button type="text" size="small" @click="onEdit(scope.row.service_id)">{{ $t("edit") }}</el-button>
            <el-button type="text" size="small" @click="onDel(scope.row.service_id, scope.$index)">{{
              $t("delete")
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>

    <!--聊天记录-->
    <el-dialog :title="$t('chatrecord')" :visible.sync="dialogTableVisible">
      <el-table v-if="isChat" key="isIndex" v-loading="loadingChat" :data="tableChatData.data">
        <el-table-column property="user.nickname" :label="$t('username')" min-width="100" />
        <el-table-column :label="$t('useravatar')" min-width="80">
          <template slot-scope="scope">
            <img v-if="scope.row.user.avatar" class="tabImage" :src="scope.row.user.avatar" />
            <img v-else class="tabImage" src="../../../assets/images/f.png" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('operation')" fixed="right" min-width="80">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="check(scope.row.uid, scope.$index)">{{
              $t("viewconversation")
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button v-if="!isChat" type="primary" size="small" @click="goBack">{{
        $t("returntochatrecord")
      }}</el-button>
      <el-table v-if="!isChat" key="isIndexs" v-loading="loadingChat" :data="tableServiceData.data">
        <el-table-column :label="$t('sender')" min-width="100">
          <template slot-scope="scope">
            <span>{{
              scope.row.send_type === 0
                ? scope.row.user.nickname
                : scope.row.service.nickname
            }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('senderspicture')" min-width="80">
          <template slot-scope="scope">
            <img v-if="scope.row.user.avatar" class="tabImage" :src="scope.row.user.avatar" />
            <img v-else class="tabImage" src="../../../assets/images/f.png" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('sendmessage')" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.msn_type === 2" class="tabImage">[{{ scope.row.msn }}]</span>
            <img v-else-if="scope.row.msn_type === 3" class="tabImage" :src="scope.row.msn" />
            <span v-else>{{ scope.row.msn }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" :label="$t('sendingtime')" min-width="100" />
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="isChat ? tableChatData.total : tableServiceData.total" @size-change="handleSizeChangeChat"
          @current-change="pageChangeChat" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  serviceListApi,
  serviceCreateApi,
  serviceUpdateApi,
  serviceStatusApi,
  serviceDeleteApi,
  serviceChatListApi,
  serviceChatUidListApi,
} from "@/api/system";
const optionsData = [
  {
    value: "1",
    label: "",
  },
  {
    value: "0",
    label: "",
  },
];
export default {
  name: "Service",
  data() {
    return {
      isChat: false,
      loadingChat: false,
      dialogTableVisible: false,
      optionsData: optionsData,
      loading: false,
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        keyword: "",
        status: "",
      },
      tableChatFrom: {
        page: 1,
        limit: 8,
      },
      tableChatData: {
        data: [],
        total: 0,
      },
      tableServiceData: {
        data: [],
        total: 0,
      },
      serviceId: 0,
      uid: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 列表
    getList() {
      this.loading = true;
      serviceListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.loading = false;
        })
        .catch((res) => {
          this.$message.error(res.message);
          this.loading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    // 聊天记录列表
    goList(id) {
      this.serviceId = id;
      this.dialogTableVisible = true;
      this.tableChatFrom.page = 1;
      this.getListChat();
      this.isChat = true;
    },
    goBack() {
      this.tableChatFrom.page = 1;
      this.getListChat();
      this.isChat = true;
    },
    check(uid) {
      this.uid = uid;
      this.serviceChatUidList(uid);
      this.isChat = false;
    },
    // 聊天记录列表
    getListChat() {
      this.loadingChat = true;
      serviceChatListApi(this.serviceId, this.tableChatFrom)
        .then((res) => {
          this.tableChatData.data = res.data.list;
          this.tableChatData.total = res.data.count;
          this.loadingChat = false;
        })
        .catch((res) => {
          this.$message.error(res.message);
          this.loadingChat = false;
        });
    },
    // 聊天记录列表
    serviceChatUidList(uid) {
      this.loadingChat = true;
      serviceChatUidListApi(this.serviceId, uid, this.tableChatFrom)
        .then((res) => {
          this.tableChatData.data = [];
          this.tableServiceData.data = res.data.list;
          this.tableServiceData.total = res.data.count;
          this.loadingChat = false;
        })
        .catch((res) => {
          this.$message.error(res.message);
          this.loadingChat = false;
        });
    },
    pageChangeChat(page) {
      this.tableChatFrom.page = page;
      this.isChat ? this.getListChat() : this.serviceChatUidList(this.uid);
    },
    handleSizeChangeChat(val) {
      this.tableChatFrom.limit = val;
      this.getListChat();
      this.isChat ? this.getListChat() : this.serviceChatUidList(this.uid);
    },
    // 添加
    onAdd() {
      this.$modalForm(serviceCreateApi()).then(() => this.getList());
    },
    // 编辑
    onEdit(id) {
      this.$modalForm(serviceUpdateApi(id)).then(() => this.getList());
    },
    onDel(id, idx) {
      this.$modalSure(this.$t("deleteservice")).then(() => {
        serviceDeleteApi(id)
          .then(({ message }) => {
            this.$message.success(message);
            this.tableData.data.splice(idx, 1);
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
    // 修改状态
    onchangeIsShow(row) {
      serviceStatusApi(row.service_id, row.status)
        .then(({ message }) => {
          this.$message.success(message);
          this.getList();
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    },
  },
};
</script>

<style scoped lang="scss">

</style>
